package yxy.game.pm2.command.therionEquipment;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.GodBeastEquipPosition;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;
import yxy.game.pm2.module.godBeast.TherionEquipHandler;

/**
 * @author wrs
 * @since 2022-07-28
 * @descrition 神兽装备升级选择
 */
@MessageType(proto = Command.TherionEquipmentUpgradeSelect ,classtype = PickedGodBeastMessage.TherionEquipmentUpgradeSelectRequest.class)
public class CMD_TherionEquipmentUpgradeSelect extends Command<PickedGodBeastMessage.TherionEquipmentUpgradeSelectRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.TherionEquipmentUpgradeSelectRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int therionType = request.getTherionType(); //神兽Type
        int equipPos = request.getEquippos(); //装备槽位置  爪：11，魂：12，环：13

        PickedGodBeastMessage.TherionEquipmentUpgradeSelectResponse.Builder response = PickedGodBeastMessage.TherionEquipmentUpgradeSelectResponse.newBuilder();

        TherionEquipHandler handler = new TherionEquipHandler();

        try {
            PickedGodBeastModule pgbModule = PickedGodBeastModule.get(playerCode);
            if (pgbModule == null) {
                send(messageSource, response, Result.DataError);//不存在,参数错误
                return;
            }

            GodBeast godBeast = pgbModule.getGbs().get(String.valueOf(therionType)); //神兽
            if (godBeast == null) {
                send(messageSource, response, Result.DataError);//参数错误
                return;
            }

            GodBeastEquipPosition equipPosition = GodBeastEquipPosition.forNumber(equipPos); //神兽装备部位
            if (equipPosition == null){
                send(messageSource, response, Result.ParamError);
                return;
            }

            response.setEquippos(equipPos);
            Result result = handler.packTherionEquipSlot(godBeast,equipPosition,response);

            send(messageSource,response,result);

        }catch (Exception e){
            e.printStackTrace();
        }


    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.TherionEquipmentUpgradeSelectResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
